package app.netant.book.bookdown;

import org.jsoup.select.Elements;

import app.netant.book.base.BaseDetail;
/**
 * 
 * @author JiaJ
 *
 */
public class BookDownDetail extends BaseDetail {
	
	private String exp = "txt";

	public BookDownDetail(String netSite) {
		super(netSite);
		this.preHref = "http://www.bookdown.com.cn";
	}

	@Override
	public void analyseTitle() {
		Elements parts = this.select(".globalBox #pageMain .mainBody .pageMainArea #downInfoArea #downInfoTitle");
		if(parts.size()>0){
			String title = parts.get(0).text();
			int st = title.indexOf("《");
			int ed = title.indexOf("》");
			if(st >=0 && ed >= 0 && ed>st){
				title = title.substring(st+1,ed);
			}
			System.out.println("【Title】:"+title);
			this.entity.setNovelName(title);
		}
	}

	@Override
	public void analyseTags() {
		Elements parts = this.select(".globalBox #pageMain .mainBody .pageMainArea #downInfoArea dd.downInfoRowL");
		if(parts.size()>1){
			String tags = parts.get(1).text();
			this.splitTags(tags, true);
		}
	}

	@Override
	public void analyseMemo() {
		Elements parts = this.select(".globalBox #pageMain .mainBody .pageMainArea .mainstory #mainSoftIntro p");
		if(parts.size()>0){
			String memo = parts.get(0).text();
			System.out.println("【Memo】:"+memo);
			this.entity.setNovelMemo(memo);
		}
	}

	@Override
	public void analyseHref() {
		Elements parts = this.select(".globalBox #pageMain .mainBody .pageMainArea .mainstory ul.downlistbox li.downAddress_li a");
		for(int i=0;i<parts.size();i++){
			String text = parts.get(i).text();
			if(text.indexOf("下载")>=0){
				String refer = parts.get(i).attr("href");
				System.out.println("【Refer】:"+refer);
				this.entity.setRefer(refer);
				
				BookDownDetail detail = new BookDownDetail(refer);
				String href = detail.stepHref();
				
				if(href.indexOf("zip")>0){
					this.exp = "zip";
				}else{
					this.exp = "txt";
				}
				
				System.out.println("【Href】:"+href);
				System.out.println("【Exp】:"+this.exp);
				
				this.entity.setDownPath(href);
				
				String title = this.entity.getNovelName();
				this.entity.setNovelFullName(title+"."+this.exp);
				
			}
		}
	}
	
	public String stepHref(){
		Elements parts = this.select(".globalBox #Frame table tbody tr td table tbody tr td a.strong.blue");
		for(int i=0;i<parts.size();i++){
			String text = parts.get(i).text();
			if(text.indexOf("下载地址")>=0){
				return parts.attr("href");
			}
		}
		
		return null;
	}
	
	@Override
	public void analyseType() {
		Elements parts = this.select("#globalNavmid #topclassmerber .li_l a");
		if(parts.size()>1){
			String type = parts.get(1).text();
			System.out.println("【Type】:"+type);
			this.entity.setNovelType(type);
		}
	}
	
	public static void main(String[] args) {
		BookDownDetail detail = new BookDownDetail("http://www.bookdown.com.cn/bookinfo/44457.html");
		detail.test();
	}



}
